<?php



namespace app\meetyou\queue;

use app\admin\queue\Queue;
use think\console\Input;
use think\console\Output;
use think\Exception;
use think\exception\PDOException;

/**
 * Class Jobs
 * @package app\meetyou
 */
class MeetyouQueue extends Queue
{

    /**
     * 当前类名
     * @var string
     */
    const URI = self::class;

    /**
     * 执行任务
     * @param Input $input
     * @param Output $output
     * @param array $data
     * @throws Exception
     * @throws PDOException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function execute(Input $input, Output $output, array $data = [])
    {
        $datetime = $this->getDatetime('meetyou_reservation_wait_time');

        //当天累计匹配单数
        $reservation_count = intval(sysconf('meetyou_reservation_order_num'));
        //每订单可匹配数量
        $meet_num = 2;

        $time = time();

        //预约订单列表
        $reservation_order_list = db('meetyou_order')->alias('o')
            ->leftJoin('meetyou_member m', 'm.id=o.mid')
            ->where([['o.status', 'eq', '0'], ['o.pay_orderid', 'eq', '0'], ['o.pay_status', 'eq', '0'], ['o.create_at', '<', $datetime]])
            ->where('m.status', '<>', 0)
            ->order(['o.reservation_order' => 'desc', 'o.create_at' => 'asc'])
            ->limit($reservation_count)
            ->select();

        $count = 0;
        if (!empty($reservation_order_list)) {
            foreach ($reservation_order_list as $order) {
                //查找匹配订单 transaction
                $transaction_order = db('meetyou_order')->alias('o')
                    ->leftJoin('meetyou_member m', 'm.id=o.mid')
                    ->where([['o.status', 'eq', '3'], ['o.meet_num', '<', $meet_num], ['m.status', '<>', '0']])
                    ->order(['o.meet_num' => 'asc', 'o.transaction_order' => 'desc', 'o.create_at' => 'asc'])
                    ->find();
                if (!empty($transaction_order)) {
                    $count++;
                    //更新预约订单
                    db('meetyou_order')->update([
                        'pay_orderid' => $transaction_order['id'],
                        'status'      => 1,
                        'update_at'   => $time,
                        'id'          => $order['id']

                    ]);

                    //更新匹配订单
                    db('meetyou_order')->update([
                        'meet_num'  => intval($transaction_order['meet_num']) + 1,
                        'update_at' => $time,
                        'id'        => $transaction_order['id']
                    ]);
                }
            }
        }

    }
    /**
     * 获取配置时间
     * @param string $code
     * @return string
     * @throws Exception
     * @throws PDOException
     */
    private function getDatetime($code) {
        $minutes = intval(sysconf($code) * 60);
        return date('Y-m-d H:i:s', strtotime("-{$minutes} minutes"));
    }

}
